from flask import Flask
# from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import os, platform


class Application(Flask):
    def __init__(self, import_name, template_folder=None, root_path=None):
        super(Application, self).__init__(import_name, template_folder=template_folder,
                                          root_path=root_path, static_folder=None)
        # 加载基础配置文件（不管本地还是线上都需要的配置）
        self.config.from_pyfile('config/base_setting.py')

        # 加载方式一：通过环境变量加载配置文件
        # if "ops_config" in os.environ:  # ops_config选择 local或者production
        #     self.config.from_pyfile('config/%s_setting.py' % os.environ['ops_config'])
        # else:
        #     # 默认运行用本地模式，即开发模式
        #     self.config.from_pyfile('config/local_setting.py')

        # 加载方式二： 通过操作系统名称 来加载环境变量
        print("当前操作系统平台:", platform.system())
        if platform.system().lower() == "windows":
            self.config.from_pyfile('config/local_setting.py')
        elif platform.system().lower() == "linux":
            self.config.from_pyfile('config/production_setting.py')
        else:
            self.config.from_pyfile('config/local_setting.py')

        # db = SQLAlchemy(app)
        db.init_app(self)


# 构建SQLARchemy对象
db = SQLAlchemy()

import os

template_folder = os.getcwd() + "/web/templates/"
print('template_folder:', template_folder)
# app=Flask(__name__)
app = Application(__name__, template_folder=template_folder, root_path=os.getcwd())
app.json.ensure_ascii = False  # 解决json中文乱码的问题
# manager = Manager(app)
"""
函数模板
"""
from common.libs.UrlManager import UrlManager

# 添加到jinja2模板中，作为全局变量
app.add_template_global(UrlManager.buildStaticUrl, "buildStaticUrl")
app.add_template_global(UrlManager.buildUrl, "buildUrl")
app.add_template_global(UrlManager.buildImageUrl, 'buildImageUrl')
